package com.wsx.ones.monitor.core.netty.codec;

import com.wsx.ones.monitor.core.codec.MessageCodec;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * 处理编码的netty核心类
 *
 * Created by wangshuaixin on 17/2/6.
 */
public class MessageEncoder extends MessageToByteEncoder<Object> {

    private MessageCodec messageCodec;

    public MessageEncoder(MessageCodec messageCodec) {
        this.messageCodec = messageCodec;
    }

    /**
     * 编码的核心实现，该方法采用不同的编码核心类注入来实现编码
     * @param channelHandlerContext
     * @param obj
     * @param byteBuf
     * @throws Exception
     */
    @Override
    protected void encode(final ChannelHandlerContext channelHandlerContext, final Object obj, final ByteBuf byteBuf) throws Exception {
        messageCodec.encode(byteBuf, obj);
    }
}
